package com.glodon.paas.databag.sso;

import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.account.api.oauth1.AccessValidationResponse;
import com.glodon.paas.account.sdk.oauth1.web.OAuthFilter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Don Li
 */
public class SecurityUtils {

    public static User getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            return null;
        }
        return (User) authentication.getDetails();
    }

    public static boolean isAuthenticated() {
        return getCurrentUser() != null;
    }
    
    public static String getOwnerId(HttpServletRequest request){
    	AccessValidationResponse response = OAuthFilter.getValidationResponse(request);
		return response.getUserId();
    }
}
